#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define db double
using namespace std;
int a[100005],c[100005];
pair<int,int> cal(int x)
{
    int mul = 1;
    int cnt = 0;
    while(mul < x)
    {
        mul *= 2;
        cnt++;
    }
    return {cnt,mul};
}
char d[5005][5005];
int ans = 0;
int DFS(int L,int R,int r,int g)
{
    //cout << L << " " << R << " " << r << " " << g << endl;
    if (r == 1)
    {
        if (d[r][g] == '1')
        {
            if (a[R] >= r)
            {
                return R;
            }
            return L;
        }
        if (a[L] >= r)
        {
            return L;
        }
        return R;
    }
    int mid = (L + R) / 2;
    int wL = DFS(L,mid,r - 1,g * 2 - 1);
    int wR = DFS(mid + 1,R,r - 1,g * 2);
    if (d[r][g] == '1')
    {
        if (a[wR] >= r)
        {
            return wR;
        }
        return wL;
    }
    if (a[wL] >= r)
    {
        return wL;
    }
    return wR;
}
void solve(int id)
{
    pair<int,int> p = cal(c[id]);
    if (p.first == 0)
    {
        ans ^= id;
        return;
    }
    if (p.second == c[id])
    {
        ans ^= (id * DFS(1,c[id],p.first,1));
        return;
    }
}
int main()
{
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    int n,m;
    scanf("%d%d",&n,&m);
    for (int i = 1;i <= n;i++)
    {
        scanf("%d",&a[i]);
    }
    for (int i = 1;i <= m;i++)
    {
        scanf("%d",&c[i]);
    }
    pair<int,int> p = cal(n);
    int num = p.second;
    for (int i = 1;i <= p.first;i++)
    {
        num /= 2;
        for (int j = 1;j <= num;j++)
        {
            cin >> d[i][j];
        }
    }
    int T;
    scanf("%d",&T);
    while(T--)
    {
        ans = 0;
        int x[4];
        for (int i = 0;i < 4;i++)
        {
            scanf("%d",&x[i]);
        }
        for (int i = 1;i <= n;i++)
        {
            a[i] = a[i] ^ x[i % 4];
        }
        for (int i = 1;i <= m;i++)
        {
            solve(i);
        }
        printf("%d\n",ans);
    }
    return 0;
}
